<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\admin\model;

use app\admin\service\User;
use app\api\model\Token;
use think\Exception;
use think\Model;

class Staff extends Model
{
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;


    public function login($param)
    {

        try {
            if (!$param['phone'] || !$param['password']) {
                throw new Exception("缺少参数");
            }
            $username = trim($param['phone']);
            $password = trim($param['password']);
            $admin = Staff::get(['phone' => $username]);
            if (!$admin) {
                throw new Exception('用户名不存在');
            }

            if ($admin->password != encrypt_password($password, $admin->encrypt)) {
                throw new Exception('密码不正确');
            }
            $wxUserMes['token'] = (new Token())->product_token($admin['id'], 1);
            $wxUserMes['uid'] = $admin->id;
            $wxUserMes['identity'] = $admin->identity;
            return ['code' => 200, 'msg' => '获取信息成功', 'data' => $wxUserMes];


        } catch (Exception $e) {
            return ['code' => 201, 'msg' => $e->getMessage()];
        }
    }


    public function getStaffInfo($param)
    {
        $userInfo = self::where(['id' => $param['uid']])->field('id,class,phone,avatar')->find();
        return ['code' => 200, 'msg' => '获取信息成功', 'data' => $userInfo];
    }


    public function staffLogin($data, $keeptime = 0)
    {
        $username = trim($data['username']);
        $password = trim($data['password']);
        $admin = Staff::get(['username' => $username]);
        if (!$admin) {
            throw new Exception('用户名不正确');
        }
        if ($admin['status'] !== 1) {
            throw new

            $this->setError('管理员已经被禁止登录');
            return false;
        }
        if ($admin->password != encrypt_password($password, $admin->encrypt)) {
            //$admin->loginfailure++;
            $admin->save();
            $this->setError('密码不正确');
            return false;
        }
        //$admin->loginfailure = 0;
        $admin->last_login_time = time();
        $admin->last_login_ip = request()->ip();
        $admin->token = Random::uuid();
        $admin->save();
        Session::set("admin", $admin->toArray());
        Session::set("admin.safecode", $this->getEncryptSafecode($admin));
        $this->keeplogin($admin, $keeptime);
        return true;
    }


    public function updatePassword($param)
    {
        try {
            $oldPassword = trim($param['oldPassword']);
            $staff = self::where(['id' => $param['uid']])->find();
            if (!$staff) {
                throw new Exception("用户不存在");
            }

            if ($staff->password != encrypt_password($oldPassword, $staff->encrypt)) {
                throw new Exception("原始密码不正确");
            }

            if (trim($param['newPassword']) != trim($param['password_confirm'])) {
                throw new Exception("两次密码输入不一致");
            }

            $passwordinfo = encrypt_password(trim($param['newPassword'])); //对密码进行处理
            $params['password'] = $passwordinfo['password'];
            $params['encrypt'] = $passwordinfo['encrypt'];

            if (!$staff->save($params)) {
                throw new Exception("修改失败请重新尝试");
            }
            return ['code' => 200, 'msg' => '修改成功', 'data' => []];
        } catch (\Exception $e) {
            return ['code' => 201, 'msg' => $e->getMessage()];
        }
    }


    public function loginOut($parma){
        $token = \think\Db::name('user_token')->where(['user_id' => $parma['uid']])->delete();
        if($token){
            return ['code'=>200,'msg'=>'退出成功','data' => []];
        }
        return ['code' => 201, 'msg' =>'退出失败'];
    }


}
